/****************************************************************************
 * Application   : Freekick Desktop Manager
 * Author        : sfreekick
 * Email         : sfreekick@gmail.com
 * Copyright     : (C) 2008 sfreekick
 * **************************************************************************/

/****************************************************************************
 *  This file is part of FreeKickManager.
 *
 *  FreeKickManager is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  FreeKickManager is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with FreeKickManager. If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FreeKickManager
{
    public partial class FrmNotas : Form
    {
        private int idJugador;

        public FrmNotas()
        {
            InitializeComponent();

            idJugador = 0;

            recargarLista();
        }

        public FrmNotas(int idJugador)
        {
            InitializeComponent();

            this.idJugador = idJugador;

            recargarLista();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FrmNuevaNota frm = new FrmNuevaNota();

            DialogResult res = frm.ShowDialog();

            if(res == DialogResult.OK)
                recargarLista();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            BDFreeKick bd = FrmPrincipal.bd;

            bd.deleteNotaPorId(Int32.Parse(lstNotas.SelectedItems[0].SubItems[0].Text));

            recargarLista();
        }

        private void recargarLista()
        {
            BDFreeKick bd = FrmPrincipal.bd;

            List<Nota> listaNotas = null;

            if(idJugador == 0)
                listaNotas = bd.selectAllNotas();
            else
                listaNotas = bd.selectNotasPorJugador(idJugador);

            lstNotas.Items.Clear();

            foreach (Nota n in listaNotas)
            {
                string nombreJugador = "";

                if (n.idJugador != 0)
                {
                    Jugador j = bd.selectJugadorCompletoPorId(n.idJugador);

                    if(j != null)
                        nombreJugador = j.ToString();
                    else
                        nombreJugador = "Antiguo";
                }
                else
                {
                    nombreJugador = "Ninguno";
                }

                ListViewItem li = new ListViewItem(new string[5] { n.id.ToString(), n.fecha.ToShortDateString(), n.idJugador.ToString(), nombreJugador, n.texto });
                lstNotas.Items.Add(li);
            }
        }

        private void lstNotas_DoubleClick(object sender, EventArgs e)
        {
            int idNota = Int32.Parse(lstNotas.SelectedItems[0].SubItems[0].Text);
            string textoNota = lstNotas.SelectedItems[0].SubItems[4].Text;
            string nombreJugador = lstNotas.SelectedItems[0].SubItems[3].Text;

            FrmNuevaNota frm = new FrmNuevaNota(idNota, nombreJugador, textoNota);
            frm.ShowDialog();
        }
    }
}